package vendas.web.action;

import gminet.infra.business.BusinessException;
import gminet.infra.web.AccessException;
import gminet.infra.web.ConfigPropertiesWEB;
import gminet.infra.web.SessionAccess;

import java.io.Serializable;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import vendas.dto.ActionType;
import vendas.dto.User;
import vendas.dto.UserType;


public class WebAction implements Serializable {

    private static final long serialVersionUID = 6688463139467955200L;

    private static String CONTENT_REAL_PATH = null;

    private ActionType actionType;
    
    private boolean confirmAction;

    public WebAction() {
    }

    public static boolean contentRealPathInitialized() {

        return CONTENT_REAL_PATH != null;
    }

    public static void initializeContentRealPath(String contentRealPath) {

        if (CONTENT_REAL_PATH == null) {
            CONTENT_REAL_PATH = contentRealPath;
        }
    }
    
    public static String getContentRealPath() {
        return WebAction.CONTENT_REAL_PATH;
    }

    public HttpSession getSession() {

        return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }

    public User getLocalUser() {

        return (User) SessionAccess.getAccessObject(getSession());
    }

    public boolean isLogado() {

        return getLocalUser() != null;
    }

    public boolean isAdministrator() {

        return getLocalUser() != null && getLocalUser().getType().getId() >= UserType.ADMINISTRATOR.getId();
    }

    public void setLocalUser(User user) {

        SessionAccess.setAccessObject(user, getSession());
    }

    public void releaseLocalUser() {

        SessionAccess.releaseAccessObject(getSession());
    }

    public void releaseSession() {

        getSession().invalidate();
    }

    public void verifyLogin() throws AccessException {

        if (getLocalUser() == null) {
            throw new AccessException("Usuário não logado no sistema.");
        }
    }

    public void verifyLogin(UserType userType) throws AccessException {

        verifyLogin();
        if (getLocalUser().getType().getId() < userType.getId()) {
            throw new AccessException("Usuário sem acesso a pagina. Acesse como " + userType.getName() + ".");
        }
    }
    
    public Date getDate() {
        
        return new Date();
    }

//    public void addMessage(String clientId, String strMessage) {
//
//        FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(strMessage));
//    }

    public void addBusinessException(String clientId, BusinessException exception) {
        
        FacesMessage message = new FacesMessage(exception.getMessage());
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        StringBuffer detail = new StringBuffer();
        detail.append("");
        Throwable cause = exception.getCause();
        while (cause != null) {
            detail.append(cause.getMessage());
            cause = cause.getCause();
        }
        message.setDetail(detail.toString());
        FacesContext.getCurrentInstance().addMessage(clientId, message);
        //
        RequestContext context = RequestContext.getCurrentInstance();
        context.addCallbackParam("hasBusinessException", true);
    }

    public ActionType getActionType() {
        if (actionType == null) {
            actionType = ActionType.UNDEFINED;
        }
        return actionType;
    }

    public void setConfirmAction(boolean confirmAction) {
        this.confirmAction = confirmAction;
    }
    
    public boolean isConfirmAction() {
        return confirmAction;
    }
    
    public boolean isActionInsert() {
        return getActionType().equals(ActionType.INSERT);
    }

    public void setActionInsert(boolean actionInsert) {

        if (actionInsert) {
            setActionType(ActionType.INSERT);
        }
    }

    public boolean isActionEdit() {
        return getActionType().equals(ActionType.EDIT);
    }

    public void setActionEdit(boolean actionEdit) {
        if (actionEdit) {
            setActionType(ActionType.EDIT);
        }
    }

    public boolean isActionDelete() {
        return getActionType().equals(ActionType.DELETE);
    }

    public void setActionDelete(boolean actionDelete) {
        if (actionDelete) {
            setActionType(ActionType.DELETE);
        }
    }

    public boolean isActionActive() {
        return getActionType().equals(ActionType.ACTIVE);
    }

    public void setActionActive(boolean actionActive) {
        if (actionActive) {
            setActionType(ActionType.ACTIVE);
        }
    }

    public boolean isActionDesactive() {
        return getActionType().equals(ActionType.DESACTIVE);
    }

    public void setActionDesactive(boolean actionDesactive) {
        if (actionDesactive) {
            setActionType(ActionType.DESACTIVE);
        }
    }

    public void setActionType(ActionType actionType) {
        this.actionType = actionType;
    }
    
    public String getAccessEmail() {
        
        return ConfigPropertiesWEB.getAccessEmail();
    }

    public String getAccessPassword() {
        
        return ConfigPropertiesWEB.getAccessPassword();
    }

}
